﻿using System;

namespace EfficientlyLazy.PhotoFramer.Repository
{
    public class SessionScope : IDisposable
    {
        private readonly object _lock;
        private bool _isDisposed;

        public SessionScope()
        {
            _isDisposed = false;
            _lock = new object();

            SessionManager.GetCurrentSession();
        }

        public void Flush()
        {
            SessionManager.FlushCurrentSession();
        }

        public void Evict(object entity)
        {
            SessionManager.Evict(entity);
        }

        public void Dispose()
        {
            if (_isDisposed == false)
            {
                lock (_lock)
                {
                    SessionManager.CloseCurrentSession();

                    _isDisposed = true;
                }
            }
        }
    }
}